﻿using System;
using System.IO;

public class StudentToBinary
{
    private string fileName;

    public StudentToBinary(string fileName)
    {
        this.fileName = fileName;
    }

    public void AddStudentToFile(Student student)
    {
        BinaryWriter writer = new BinaryWriter(new FileStream(fileName, FileMode.Append));
        using (writer)
        {
            writer.Write(student.ToString());
            writer.Write(Environment.NewLine);
        }
    }

    public void DeleteStudent(int facultyNumberToBeDeleted)
    {
        BinaryReader reader = new BinaryReader((new FileStream(fileName, FileMode.Open)));
        long positionToBeDeleted = -1;
        using (reader)
        {
            long currentPosition = reader.BaseStream.Position;
            while (reader.BaseStream.Position < reader.BaseStream.Length)
            {
                string student = reader.ReadString();
                if (student != Environment.NewLine)
                {
                    int facultyNumber = ExtractFacultyNumber(student);
                    if (facultyNumberToBeDeleted == facultyNumber)
                    {
                        positionToBeDeleted = currentPosition;
                    }
                }
                currentPosition = reader.BaseStream.Position;
            }
        }
        if (positionToBeDeleted != -1)
        {
            DeleteStudent(positionToBeDeleted);
        }
    }

    private void DeleteStudent(long postionToBeDeleted)
    {
        string tempFileName = Path.GetTempFileName();
        BinaryReader reader = new BinaryReader((new FileStream(fileName, FileMode.Open)));
        using (reader)
        {
            BinaryWriter writer = new BinaryWriter(new FileStream(tempFileName, FileMode.Append));
            using (writer)
            {
                while (reader.BaseStream.Position < reader.BaseStream.Length)
                {
                    if (reader.BaseStream.Position == postionToBeDeleted)
                    {
                        reader.ReadString();
                        reader.ReadString();
                        continue;
                    }
                    else
                    {
                        string stringToBeWritten = reader.ReadString();
                        writer.Write(stringToBeWritten);
                    }
                }
            }            
        }
        File.Delete(fileName);
        File.Move(tempFileName, fileName);
    }

    public void PrintStudents()
    {
        BinaryReader reader = new BinaryReader((new FileStream(fileName, FileMode.Open)));
        using (reader)
        {
            while (reader.BaseStream.Position < reader.BaseStream.Length)
            {
                string student = reader.ReadString();
                if (student == Environment.NewLine)
                {
                    continue;
                }
                Console.WriteLine(student);
            }
        }
    }

    private int ExtractFacultyNumber(string student)
    {
        string[] studentInfo = student.Split(' ');
        int facultyNumber = int.Parse(studentInfo[studentInfo.Length - 1]);
        return facultyNumber;
    }
}